package com.itfitness.stretchcalendarview.widget.calendarview.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import com.itfitness.stretchcalendarview.R
import com.itfitness.stretchcalendarview.utils.CalendarUtil
import com.itfitness.stretchcalendarview.widget.calendarview.adapter.StretchCalendarAdapter
import com.itfitness.stretchcalendarview.widget.calendarview.bean.CalendarDateBean
import java.util.ArrayList
import java.util.Calendar

/**
 * 伸缩日历
 */
class StretchCalendarView: FrameLayout {
    /**
     * 日期选择的回调
     */
    interface OnDateSelectCallBack{
        fun onDateSelect(year:Int,month:Int,day:Int)
    }
    var mSelectYear = 0
    var mSelectMonth = 0
    var mSelectDay = 0
    var mStretch:Boolean = false
    private lateinit var wgvCalendar: WrapGridView

    private var calendarAdapter: StretchCalendarAdapter? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){
        View.inflate(context, R.layout.view_stretchcalendar,this)
        initView()
    }

    private fun initView() {
        wgvCalendar = findViewById(R.id.wgvCalendar)
    }

    fun initData(year:Int,month: Int,day:Int){
        mSelectYear = year
        mSelectMonth = month - 1
        mSelectDay = day
        stretch()
    }

    fun stretch(){
        val data = ArrayList<CalendarDateBean>()
        mStretch = !mStretch
        if(mStretch){
            val lineNum = Math.floor((mSelectDay + getMonthOneDayWeek() - 1) / 7.0 - 0.1).toInt()
            if(lineNum == 0){
                //获取第一天是星期几然后计算出需要填充的空白数据
                repeat(getMonthOneDayWeek() - 1){
                    //填充空白的
                    data.add(CalendarDateBean(0,0,0,"",""))
                }
                repeat(7 - getMonthOneDayWeek() + 1){
                    val lunar = CalendarUtil.solarToLunar(mSelectYear,mSelectMonth + 1,it +1)
                    val festival = CalendarUtil.solarToFestival(mSelectYear,mSelectMonth + 1,it +1)
                    data.add(CalendarDateBean(mSelectYear,mSelectMonth,it +1,lunar,festival))
                }
            }else{
                val monthDayWeek = getMonthDayWeek()
                var addDay = mSelectDay - monthDayWeek
                val monthMaxDay = getMonthMaxDay()
                //填充日期数据
                repeat(7){
                    addDay++
                    if(addDay <= monthMaxDay){
                        val lunar = CalendarUtil.solarToLunar(mSelectYear,mSelectMonth + 1,addDay)
                        val festival = CalendarUtil.solarToFestival(mSelectYear,mSelectMonth + 1,addDay)
                        data.add(CalendarDateBean(mSelectYear,mSelectMonth,addDay,lunar,festival))
                    }
                }
            }
        }else{
            //获取第一天是星期几然后计算出需要填充的空白数据
            repeat(getMonthOneDayWeek() - 1){
                //填充空白的
                data.add(CalendarDateBean(0,0,0,"",""))
            }
            //填充日期数据
            repeat(getMonthMaxDay()){
                val lunar = CalendarUtil.solarToLunar(mSelectYear,mSelectMonth + 1,it + 1)
                val festival = CalendarUtil.solarToFestival(mSelectYear,mSelectMonth + 1,it + 1)
                data.add(CalendarDateBean(mSelectYear,mSelectMonth,it + 1,lunar,festival))
            }
        }
        calendarAdapter = StretchCalendarAdapter(mSelectYear,mSelectMonth,mSelectDay,data,context)
        calendarAdapter?.onDateSelectCallBack = object : OnDateSelectCallBack{
            override fun onDateSelect(year: Int, month: Int, day: Int) {
                mSelectYear = year
                mSelectMonth = month - 1
                mSelectDay = day
            }
        }
        wgvCalendar.adapter = calendarAdapter
    }


    /**
     * 获取第一天为星期几
     */
    private fun getMonthOneDayWeek(): Int {
        val a: Calendar = Calendar.getInstance()
        a.set(Calendar.YEAR, mSelectYear)
        a.set(Calendar.MONTH, mSelectMonth)
        a.set(Calendar.DATE, 1) //把日期设置为当月第一天
        return a.get(Calendar.DAY_OF_WEEK)
    }

    /**
     * 获取选中的为星期几
     */
    private fun getMonthDayWeek(): Int {
        val a: Calendar = Calendar.getInstance()
        a.set(Calendar.YEAR, mSelectYear)
        a.set(Calendar.MONTH, mSelectMonth)
        a.set(Calendar.DATE, mSelectDay) //把日期设置为当月第一天
        return a.get(Calendar.DAY_OF_WEEK)
    }

    /**
     * 获取第一天为星期几
     */
    private fun getMonthLastDayWeek(): Int {
        val a: Calendar = Calendar.getInstance()
        a.set(Calendar.YEAR, mSelectYear)
        a.set(Calendar.MONTH, mSelectMonth)
        a.set(Calendar.DATE, getMonthMaxDay()) //把日期设置为当月第一天
        return a.get(Calendar.DAY_OF_WEEK)
    }

    /**
     * 获取当月有几天
     */
    private fun getMonthMaxDay(): Int {
        val a: Calendar = Calendar.getInstance()
        a.set(Calendar.YEAR, mSelectYear)
        a.set(Calendar.MONTH, mSelectMonth)
        return a.getActualMaximum(Calendar.DAY_OF_MONTH)
    }

    /**
     * 获取当月有几天
     */
    private fun getMonthMaxDay(year:Int,month:Int): Int {
        val a: Calendar = Calendar.getInstance()
        a.set(Calendar.YEAR, year)
        a.set(Calendar.MONTH, month)
        return a.getActualMaximum(Calendar.DAY_OF_MONTH)
    }
}